package app.tasks.schedule;

import app.database.CrubUtils;
import app.env.Data;
import app.env.Position;
import app.agent.Schedule;
import app.util.AppUtils;
import jade.core.AID;
import jade.core.behaviours.OneShotBehaviour;
import jade.lang.acl.ACLMessage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Name: dealFindTarget
 * @Description:
 * @Author: colfish
 * @Date: 2021/4/20
 */
public class DealFindTarget extends OneShotBehaviour {


    private static final long serialVersionUID = -4797421718263108372L;
    private Logger logger = LogManager.getLogger(this.getClass());

    private Schedule schedule;

    int nextState;

    public DealFindTarget(Schedule schedule){
        nextState = 2;
        this.schedule = schedule;
    }

    @Override
    public void action() {
        // 存放目标代理位置
//        long stime = System.currentTimeMillis();
        Position targetPosition = schedule.tempTarPos;
        // 如果新找到的目标代理没有被找到过
        if(!Data.foundTargets.contains(targetPosition)){
            String targetName = "";
            for(AID aid : Data.targetPositions.keySet()){
                if(Data.targetPositions.get(aid).equals(targetPosition)){
                    targetName = aid.getLocalName();
                }
            }
            // 减去包括找到目标的搜索代理在内的4个计数，使回到WaitMessage后，可以判断receiveCount的值，来决定是否进行对信号排序
            Data.foundTargets.add(targetPosition);
            // 找到目标代理上下左右的四个目标点
            Position[] positions = AppUtils.findSurroundTarget(targetPosition);
            // 分别找到距离四个目标点最近的四个搜索代理名称，存放到informSearchPos Map中，并将四个搜索代理从searchAgentPositions Map中移除
            for(Position position : positions){
                String name = AppUtils.findMinTarget(position, Data.searchAgentPositions);
                if(Data.receivedSearch.contains(name)){
                    schedule.receiveCount--;
                }

                new CrubUtils().saveMatchs(targetName, name);

                // 被设置为移动到目标代理附近目标点的4个搜索代理，需要从searchAgentPositions和SearchSignals中删除
                Data.searchAgentPositions.remove(name);
                Data.searchSignals.remove(name);
                // TODO 考虑直接使用agentControllers关闭搜索代理
                // 给搜索代理发送消息，使搜索代理移动到指定位置
                ACLMessage informMessage = new ACLMessage(ACLMessage.INFORM);
                AID receiver = new AID(name, AID.ISLOCALNAME);
                // 发送SEARCH_MOVE消息+要移动到的位置
                informMessage.addReceiver(receiver);
                informMessage.setContent(Data.SEARCH_MOVE_TARGET + ";" + position.toString());
                schedule.send(informMessage);
                logger.info(AppUtils.logMessage(schedule.getAID(), receiver, informMessage));
            }
        }else{
            // 重新使搜索代理执行搜索功能，给管理员重新发送搜索到的信号
            ACLMessage informMessage = new ACLMessage(ACLMessage.INFORM);
            AID receiver = schedule.tempSearchAID;
            // 发送SEARCH_AGAIN消息
            informMessage.addReceiver(receiver);
            informMessage.setContent(Data.SEARCH_AGAIN);
            schedule.send(informMessage);
            logger.info(AppUtils.logMessage(schedule.getAID(), receiver, informMessage));
        }
//        long etime = System.currentTimeMillis();
//
//        Data.Ftime = Data.Ftime + (etime - stime);
//        Data.Fcnt++;
    }

    public int onEnd(){
        return nextState;
    }
}
